﻿﻿/* **********************************************************************************
 * Copyright (c) 2011 John Hughes
 *
 * j9Script is licenced under the Microsoft Reciprocal License (Ms-RL).
 *
 * Project Website: http://j9Script.codeplex.com/
 * **********************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Reflection;

namespace J9Script
{

    public class ScriptObjectTypeInfoCollection : IEnumerable
    {
        Dictionary<string, ScriptObjectTypeInfo> _items = new Dictionary<string, ScriptObjectTypeInfo>(StringComparer.OrdinalIgnoreCase);

        public ScriptObjectTypeInfo this[string typeName]
        {
            get { return _items[typeName]; }
        }

        public void Add(ScriptObjectTypeInfo oi)
        {
            _items.Add(oi.IdentifierName, oi);
        }

        public void Add(Type t, string identifierName)
        {
            Add(new ScriptObjectTypeInfo(t, identifierName));
        }

        public void Add(Assembly assembly, string fullTypeName, string identifierName)
        {
            Add(new ScriptObjectTypeInfo(assembly, fullTypeName, identifierName));
        }

        public int Count
        {
            get { return _items.Count; }
        }

        public bool Contains(string variablename)
        {
            return _items.ContainsKey(variablename);
        }

        public void Remove(string variablename)
        {
            _items.Remove(variablename);
        }

        public void Clear()
        {
            _items.Clear();
        }

        #region Implementation of IEnumerable
        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)_items.Values.GetEnumerator();
        }
        #endregion
    }


    public class ScriptObjectTypeInfo
    {
        public Assembly Assembly;      // The assembly the contains the type.
        public string FullTypeName;    // ex: "J9Script.Scripting.ScriptDictionary"
        public string IdentifierName;  // ex: "Dictionary"

        public ScriptObjectTypeInfo(Type t, string identifierName)
        {
            Assembly = t.Assembly;
            FullTypeName = t.FullName;
            IdentifierName = identifierName;
        }

        public ScriptObjectTypeInfo(Assembly assembly, string fullTypeName, string identifierName)
        {
            Assembly = assembly;
            FullTypeName = fullTypeName;
            IdentifierName = identifierName;
        }
    }
}
